var num = 0;
var i = 0;
var PicturePath1 = "";
var PicturePath2 = "";
var PicturePath3 = "";
var form;

jQuery(function () {
    layui.use(['layer', 'form'], function () {
        form = layui.form();
        init();
        doubleClick("cardFront",function(obj){
            $(obj).attr("src","");
            $(obj).parent().hide();
            $("#filePicker").show();
        });
        doubleClick("cardBack",function(obj){
            $(obj).attr("src","");
            $(obj).parent().hide();
            $("#filePicker").show();
        });

    });
});
function init() {

    $("#btn").click(next);
}

var b = false;
function next() {
    if ($("#buyerName").val() == null || $("#buyerName").val() == '') {
        easyAlert("请填写姓名！");
        return false;
    }
    if ($("#buyerTel").val() == null || $("#buyerTel").val() == '') {
        easyAlert("请填写手机号码！");
        return false;
    }
    if ($("#compName").val() == null || $("#compName").val()== '') {
        easyAlert("请填写公司名称！");
        return false;
    }
    if($("#cardFront").attr("src") == "" || $("#cardBack").attr("src") == "")
    {
        easyAlert("请上传名片正反面照片");
        return false;
    }

    if(b){
        return;
    }
    b = true;
    $.ajax({
        url: server_url + '/rest/buyer/demand/submitAuth.do?userId='+userid
        +"&userid="+userid+"&cardFront="+$("#cardFront").attr("src")+"&cardBack="+$("#cardBack").attr("src")+"&ran="+Math.random(),
        data: $("form").serialize(),
        type:"post",
        success: function (data) {
            easyAlert("提交成功，请等待审核通过！");
            window.location.href = server_url + '/buyer/product/index.do';
        },
        error:function(){
            easyAlert("出了点问题")
            b = false;
        }
    })
}
function InitParentCate() {
    $.ajax({
        url: server_url + '/rest/productCate/selectParentCate.do',
        data:{rand:Math.random()},
        type: 'get',
        async: false,
        success: function (data) {
            if (data.retCode == "0000") {
                for (var i = 0; i < data.data.length; i++) {
                    $("#parentId1").append("<option  value='" + data.data[i].cateId + "'>" + data.data[i].cateName + "</option>");
                }
                form.render("select")
                form.on("select(parentId)", function (data) {
                    var pro = data.value;
                    $("#parentId").val(pro);
                    sencondCateLoad();
                })
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

function sencondCateLoad() {
    var parentId = $("#parentId").val();
    $.ajax({
        url: server_url + '/rest/productCate/selectChildCateKv.do?cateId=' + parentId,
        data:{rand:Math.random()},
        type: 'post',
        success: function (data) {
            $("#cateId1").html("");
            if (data.retCode == "0000") {
                var html = '<option value="">请选择</option>';
                for (var i = 0; i < data.data.length; i++) {
                    html += "<option value=" + data.data[i].cateId + ">" + data.data[i].cateName + "</option>";
                }
                $(html).appendTo("#cateId1");
                form.render("select")
                form.on("select(cateId)", function (data) {
                    var pro = data.value;
                    $("#cateId").val(pro);
                    changeSpecId();
                    //changeProductName();
                });
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

//切换子分类选择属性名称
function changeSpecId() {
    var cateId = $("#cateId").val();
    $.ajax({
        url: server_url + '/rest/product/selectByCateId.do?ran=' + Math.random(),
        type: 'post',
        data: {cateId: cateId},
        success: function (data) {
            if (data.retCode == "0000") {
                InitSpec(data);
                form.render("select")
                form.on("select(specId)", function (data) {
                    var pro = data.value;
                    $("#specId").val(pro);
                })
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

//输出Dom
function InitSpec(data) {
    var html = '<option value="">请选择</option>';
    for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].specName == "类别") {
            html += "<option value=" + data.data[i].specValueId + ">" + data.data[i].specValueName + "</option>";
        }
    }
    $("#specId1").html(html);
}

function toSkuProductList() {
    window.location.href = "index.do?ran=" + Math.random();
};